#!/usr/bin/ruby -d

require 'net/smtp'
require 'reminder'


class TaskScheduler

  def initialize(db)
    @storage = db
  end

  def send_all_reminders
    current_tasks = Array.new
    now = Time.new
    current_date = now.strftime("%Y-%m-%d")
    current_time = now.strftime("%H:%M")
    raise "No tasks for today" if !(current_tasks = @storage.get_current_tasks(current_date, current_time))
    current_tasks.each do |task|
      rem = Reminder.new(task[1], task[2])
      effect = rem.send
      if (effect =~ /^250 2.0.0/)
        @storage.reset_current_tasks(current_date, current_time)
      else
        raise RuntimeError, "Can't send message: #{effect}"
      end
    end
  end
end
